package com.saibo.spider.gather;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.saibo.spider.commons.ConnectServer;
import com.saibo.spider.pojo.EntryInfo;
import com.saibo.spider.pojo.SinaWeibo;
import com.saibo.spider.utils.FileUtil;
import com.saibo.spider.utils.HttpRequestUtil;
import com.saibo.spider.utils.HttpResponseUtil;
import com.saibo.spider.utils.MD5Util;
import com.saibo.spider.utils.SinaBlogScriptUtil;
import com.saibo.spider.utils.StringUtil;

/**
 * 请求、解析新浪博客微博页面的模板
 * 
 * @author TerransForce
 *
 */
public class MouldSinaBlog {
	
	
	
	public static void main(String[] args) {
		MouldSinaBlog msb = new MouldSinaBlog();
		msb.connection("http://weibo.com/1192966660/Ek8n5BdMj?from=page_1005051192966660_profile&wvr=6&mod=weibotime&type=comment");
	}
	
	public void startM(String url){
		System.out.println("调用了");
		connection(url);
	}

	public void connection(String url) {
		if(StringUtil.isNullOrEmpty(url)){
			System.out.println("空");
			return ;
		}
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, requestHeader());
		String html = HttpResponseUtil.getHtml(httpResponse);
		if(StringUtil.isNullOrEmpty(html)){
			return;
		}
		Elements scriptElement = Jsoup.parse(html).select("script");
		for (String element : SinaBlogScriptUtil.replaceScript(scriptElement)) {
			SinaWeibo weibo = new SinaWeibo();
			String surl = filterUrl(url);
			weibo.setRowkey(MD5Util.GET16BITMD5(surl));
			weibo.setUrl(surl);
			Map<String, Object> mso = StringUtil.jsonToMap(element);
			if ("pl.content.weiboDetail.index".equals(mso.get("ns").toString())) {
				String httt = mso.get("html").toString();
				Document document = Jsoup.parse(httt);
				Element root_element = document.select("div.WB_feed>div>div.WB_cardwrap").first();
				String mid = root_element.attr("mid");
				weibo.setMid_(mid);
				String omid = root_element.attr("omid");
				String isforward = root_element.attr("isforward");
				System.out.println("是否是转发的文章:" + isforward);
				if (!StringUtil.isNullOrEmpty(isforward) && isforward.equals("1")) {
					weibo.setIsforward("1");
				} else {
					weibo.setIsforward("0");
				}
				// 从根目录开始解析
				Element wb_feed_detail = root_element.select("div>div.WB_feed_detail").first();
				Element userImgEl = wb_feed_detail.select("div>div.WB_face").first();
				FileUtil.writeLogToTxt(root_element.toString(),"D:/newSinablog1");
				Element face_El = userImgEl.select("div>div.face>a").first();
				String nickname = face_El.attr("title");
				String userurl = face_El.attr("href");
				//System.out.println(nickname + "   " + userurl);
				weibo.setAuthor_rowkey(MD5Util.GET16BITMD5(MouldSinaUserCard.getRealUserIndexUrl(userurl)));
				Element userimgEl = face_El.select("a>img").first();
				String userimgurl = userimgEl.attr("src");
				String usercard = userimgEl.attr("usercard");
				//System.out.println(userimgurl);

				//System.out.println("http://weibo.com/aj/v6/user/newcard?ajwvr=6&" + usercard);

				Element wb_detail = wb_feed_detail.select("div>div.WB_detail").first();
				Element userEle = wb_detail.select("div>div.WB_info").first();
				if (null != userEle.select("div>a>em.W_icon")) {
					//System.out.println("会员");
				}
				if (null != userEle.select("div>a>i.W_icon")) {
					//System.out.println(userEle.select("div>a>i.W_icon").attr("title"));
				}

				//System.out.println(mid + "  " + omid);
				Element platEle = wb_detail.select("div>div.WB_from>a").first();
				//System.out.println("发文日期:" + platEle.attr("date"));
				weibo.setCreate_time(platEle.attr("date"));
				
				//System.out.println("微博地址:http://weibo.com" + platEle.attr("href"));
				weibo.setUrl(MouldSinaUserCard.getRealUserIndexUrl("http://weibo.com" + platEle.attr("href")));
				if (wb_detail.select("div>div.WB_from>a").size() > 1) {
					String fromType = StringUtil.element2text(wb_detail.select("div>div.WB_from>a").get(1));
					//System.out.println("发文平台:" + fromType);
					weibo.setPlatform(fromType);
				}
				// 采集微博内容
				Element wbConEle = wb_detail.select("div>div.WB_text").first();
				String wb_con = StringUtil.element2text(wbConEle);
				//System.out.println("微博内容:" + wb_con);
				weibo.setContent(wb_con);
				// 监测是否有转发的内容
				Elements wb_feed_expand = wb_detail.select("div>div.WB_feed_expand>div.WB_expand");
				if (wb_feed_expand.size() > 0) {
					//System.out.println("-----------------------------------------------------------");
					//System.out.println("转发自:");
					
					
					SinaWeibo forwWeibo = new SinaWeibo();
					
					Element wb_info = wb_feed_expand.select("div>div.WB_info").first();
					Element wb_user = wb_info.select("div>a").first();
					String ex_nickname = wb_user.attr("nick-name");
					String ex_url = wb_user.attr("href");
					String ex_usercard = wb_user.attr("usercard");
					if (null != wb_info.select("div>a>i")) {
						//System.out.println(wb_info.select("div>a>i").attr("title"));
					}
					if (null != wb_info.select("div>a>em")) {
						//System.out.println("微博会员");
					}
					// content
					Element wb_text = wb_feed_expand.select("div>div.WB_text").first();
					Element wb_ex_forward = wb_feed_expand.select("div>div.WB_func>div.WB_handle>ul>li").first();
					Element wb_ex_comment = wb_feed_expand.select("div>div.WB_func>div.WB_handle>ul>li").get(1);
					Element wb_ex_like = wb_feed_expand.select("div>div.WB_func>div.WB_handle>ul>li").get(2);
					String forward_str = StringUtil.element2text(wb_ex_forward.select("li>span>a>span>em").get(1));
					String comment_str = StringUtil.element2text(wb_ex_comment.select("li>span>a>span>em").get(1));
					String like_str = StringUtil.element2text(wb_ex_like.select("li>span>a>span>em").get(1));
					// plat and time
					Element timeEl = wb_feed_expand.select("div>div.WB_func>div.WB_from>a").first();
					String forward_time = timeEl.attr("date");
					String wb_href = "http://weibo.com" + timeEl.attr("href");
					Element platEl = wb_feed_expand.select("div>div.WB_func>div.WB_from>a").get(1);
					
					//System.err.println(wb_href);
					weibo.setSource_rowkey(MD5Util.GET16BITMD5(filterUrl(wb_href)));
					
					//统计字段
					//System.out.println("昵称:" + ex_nickname + "  主页地址:http://weibo.com" + ex_url+ "  名片请求地址:http://weibo.com/aj/v6/mblog/info/big?ajwvr=6&" + ex_usercard);
					//System.out.println("微博内容:" + StringUtil.element2text(wb_text));
					//System.out.println("转发数:" + forward_str + "\t评论数:" + comment_str + "\t点赞数:" + like_str);
					//System.out.println("发布时间戳:" + forward_time + "\t文章地址:" + wb_href);
					//System.out.println("发文工具:" + StringUtil.element2text(platEl));
					forwWeibo.setRowkey(MD5Util.GET16BITMD5(filterUrl(wb_href)));
					forwWeibo.setUrl(filterUrl(wb_href));
					forwWeibo.setMid_(omid);
					forwWeibo.setCreate_time(forward_time);
					forwWeibo.setContent(StringUtil.element2text(wb_text));
					forwWeibo.setPlatform(StringUtil.element2text(platEl));
					String authorUrl = "http://weibo.com" + ex_url;
					forwWeibo.setAuthor_rowkey(MD5Util.GET16BITMD5(filterUrl(authorUrl)));
					forwWeibo.setIsforward("0");
					forwWeibo.setForward_count(forward_str);
					forwWeibo.setComment_count(comment_str);
					forwWeibo.setLike_count(like_str);
					Map<String,String> ss = new HashMap<String,String>();
					ss.put("jsonData", StringUtil.objectToJson(forwWeibo));
					//System.err.println(StringUtil.objectToJson(forwWeibo));
					//ConnectServer.sendPostRequest("http://192.168.1.177:8080/stream/dataSubmit/addData",StringUtil.objectToJson(ss));
					HttpRequestUtil.postRequestMethod("http://192.168.1.177:8080/stream/dataSubmit/addData", null, ss);
					//System.out.println("-----------------------------------------------------------");
				}

				// 获取文章的点赞阅读

				Element wb_feed_handle = root_element.select("div>div.WB_feed_handle").first();
				Element zf_handle = wb_feed_handle
						.select("div>div.WB_handle>ul.WB_row_line>li>a[action-type=fl_forward]>span.pos>span.line>span>em")
						.get(1);
				System.err.println(wb_feed_handle
						.select("div>div.WB_handle>ul.WB_row_line>li>a[action-type=fl_forward]"));
				Element pl_handle = wb_feed_handle
						.select("div>div.WB_handle>ul.WB_row_line>li>a[action-type=fl_comment]>span.pos>span.line>span>em")
						.get(1);
				Element zz_handle = wb_feed_handle
						.select("div>div.WB_handle>ul.WB_row_line>li>a[action-type=fl_like]>span.pos>span.line>span>em")
						.get(1);
				String forward_str = StringUtil.element2text(zf_handle);
				String comment_str = StringUtil.element2text(pl_handle);
				String like_str = StringUtil.element2text(zz_handle);
				forward_str = forward_str.replaceAll("转发", "");
				comment_str = comment_str.replaceAll("评论", "");
				like_str = like_str.replaceAll("赞", "");
				//System.out.println("转发数:" + forward_str + "\t评论数:" + comment_str + "\t点赞数:" + like_str);
				weibo.setForward_count(getInteger(forward_str)+"");
				weibo.setComment_count(getInteger(comment_str)+"");
				weibo.setLike_count(getInteger(like_str)+"");
				System.out.println(StringUtil.objectToJson(weibo));
				
				EntryInfo e = new EntryInfo();
				e.setActionname("MouldSinaBlog");
				e.setScore(0.0);
				e.setSourceid(0);
				e.setTaskid(1);
				e.setUrl("4047987103214164__b7a959e8d66e8e25");
				List<EntryInfo> le = new ArrayList<EntryInfo>();
				le.add(e);
				System.out.println(StringUtil.objectToJson(le));
				
				
				//http://192.168.1.177:8080/stream/dataSubmit/addData
				String ss = ConnectServer.sendPostRequest("http://192.168.1.161:8080/services/queuemgr/addentry",StringUtil.objectToJson(le));
				System.out.println(ss);
				
			}
		}
	}

	public int getInteger(String num) {
		if (StringUtil.isNullOrEmpty(num)) {
			return 0;
		} else {
			try {
				return Integer.parseInt(num);
			} catch (Exception e) {
				System.out.println("转换错误" + num);
				return 0;
			}
		}
	}
	/**
	 * 
	 */
	public String filterUrl(String url){
		if(url.contains("?")){
			String[] surl = url.split("\\?");
			return surl[0];
		}
		return url;
	}
	
	

	public static Map<String, String> requestHeader() {
		Map<String, String> requestHeader = new HashMap<String, String>();
		requestHeader.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		requestHeader.put("Accept-Encoding", "gzip, deflate, sdch");
		requestHeader.put("Accept-Language", "zh-CN,zh;q=0.8");
		requestHeader.put("Cache-Control", "max-age=0");
		requestHeader.put("Connection", "keep-alive");
		requestHeader.put("Cookie",FileUtil.txt2String(new File("C:/sinalogincookie.txt")));
		requestHeader.put("Host", "weibo.com");
		requestHeader.put("Upgrade-Insecure-Requests", "1");
		requestHeader.put("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
		return requestHeader;
	}
}
